package com.xh.core.aspect;

import com.xh.bussiness.course.bean.request.CourseRequest;
import com.xh.bussiness.permission.bean.dto.UserDataRuleBean;
import com.xh.bussiness.permission.service.IUserService;
import com.xh.bussiness.system.bean.dto.DictionaryBean;
import com.xh.bussiness.tiku.bean.request.PaperQueryRequest;
import com.xh.bussiness.tiku.bean.request.QuestionQueryRequest;
import com.xh.core.util.StringUtils;
import com.xh.core.util.UserUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.stream.Collectors;

/**
 * 数据权限处理器
 * @author Pitt Cheung
 */
@Aspect
@Component
public class DataPermissionAspect {

    public static final Logger log = LoggerFactory.getLogger(DataPermissionAspect.class);

    @Autowired
    private IUserService userService;
    /**
     * 试卷筛选
     */
    @Pointcut("execution(* com.xh.bussiness.tiku.service.impl.PaperServiceImpl.queryPaperPage(..))")
    public void checkQueryPaperPage(){}

    /**
     * 试题筛选
     */
    @Pointcut("execution(* com.xh.bussiness.tiku.service.impl.QuestionServiceImpl.queryQuestionPage(..))")
    public void checkQueryQuestionPage() {}

    /**
     * 课程筛选
     */
    @Pointcut("execution(* com.xh.bussiness.course.service.impl.CourseServiceImpl.queryPage(..))")
    public void checkQueryCoursePage() {}

    /**
     * 查询试卷前置处理
     * @param joinPoint
     */
    @Before("checkQueryPaperPage()")
    public void doBeforeQueryPaper(JoinPoint joinPoint) {
        log.info("---Before method {} invoke, param: {}---", joinPoint.getSignature().toShortString(),joinPoint.getArgs());

        Object[] obj = joinPoint.getArgs();
        PaperQueryRequest condition = (PaperQueryRequest) obj[1];

        //页面选择了年份或年级条件则不按数据权限查询
        if (!StringUtils.isEmpty(condition.getYear()) && !StringUtils.isEmpty(condition.getGradeId())) {
            return;
        }
        UserDataRuleBean userDataRuleBean = userService.userDataRules(condition.getCreatorId(), condition.getSubjectId());
        if (userDataRuleBean == null) {
            return;
        }
        //未选择年份，按用户所在组或自己的权限进行查询
        if (StringUtils.isEmpty(condition.getYear())) {
            condition.setYears(CollectionUtils.isEmpty(userDataRuleBean.getYears()) ? new ArrayList<>() : userDataRuleBean.getYears().stream().map(DictionaryBean::getId).collect(Collectors.toList()));
        }
        //未选择年级，按用户所在组或自己权限进行查询
        if (StringUtils.isEmpty(condition.getGradeId())) {
            condition.setGradeIds(CollectionUtils.isEmpty(userDataRuleBean.getGrades()) ? new ArrayList<>() : userDataRuleBean.getGrades().stream().map(DictionaryBean::getId).collect(Collectors.toList()));
        }
    }

    /**
     * 查询试题前置处理
     * @param joinPoint
     */
    @Before("checkQueryQuestionPage()")
    public void doBeforeQueryQuestion(JoinPoint joinPoint) {
        log.info("---Before doBeforeQueryQuestion {} invoke, param: {}---", joinPoint.getSignature().toShortString(),joinPoint.getArgs());
/*
        Object[] obj = joinPoint.getArgs();
        QuestionQueryRequest condition = (QuestionQueryRequest) obj[1];

        //页面选择了年份或年级条件则不按数据权限查询
        if (!CollectionUtils.isEmpty(condition.getYears()) && !CollectionUtils.isEmpty(condition.getGradeIds())) {
            return;
        }*/
       /* UserDataRuleBean userDataRuleBean = userService.userDataRules(UserUtils.getCurrentUserId(), condition.getSubject());
        if (userDataRuleBean == null) {
            return;
        }*/
        //未选择年份，按用户所在组或自己的权限进行查询
       /* if (CollectionUtils.isEmpty(condition.getYears())) {
            condition.setYears(CollectionUtils.isEmpty(userDataRuleBean.getYears()) ? new ArrayList<>() : userDataRuleBean.getYears().stream().map(DictionaryBean::getId).collect(Collectors.toList()));
        }
        //未选择年级，按用户所在组或自己权限进行查询
        if (CollectionUtils.isEmpty(condition.getGradeIds())) {
            condition.setGradeIds(CollectionUtils.isEmpty(userDataRuleBean.getGrades()) ? new ArrayList<>() : userDataRuleBean.getGrades().stream().map(DictionaryBean::getId).collect(Collectors.toList()));
        }*/
    }


    /**
     * 查询课程前置处理
     */
    @Before("checkQueryCoursePage()")
    public void doBeforeQueryCoursePage(JoinPoint joinPoint) {
        log.info("---Before doBeforeQueryCoursePage {} invoke, param: {}---", joinPoint.getSignature().toShortString(),joinPoint.getArgs());

        Object[] obj = joinPoint.getArgs();
        CourseRequest condition = (CourseRequest) obj[1];

        UserDataRuleBean userDataRuleBean = userService.userDataRules(UserUtils.getCurrentUserId(), condition.getSubjectId());
        if (userDataRuleBean == null) {
            return;
        }
        if (!StringUtils.isEmpty(condition.getYear()) && !StringUtils.isEmpty(condition.getSemesterId())
                && !StringUtils.isEmpty(condition.getCourseTypeId()) && !StringUtils.isEmpty(condition.getGradeId())) {
            return;
        }
        //未选择年份，按用户所在组或自己的权限进行查询
        if (StringUtils.isEmpty(condition.getYear())) {
            condition.setYears(CollectionUtils.isEmpty(userDataRuleBean.getYears()) ? new ArrayList<>() : userDataRuleBean.getYears().stream().map(DictionaryBean::getId).collect(Collectors.toList()));
        }

        //未选择学期，按用户所在组或自己的权限进行查询
        if (StringUtils.isEmpty(condition.getSemesterId())) {
            condition.setSemesterIds(CollectionUtils.isEmpty(userDataRuleBean.getTerms()) ? new ArrayList<>() : userDataRuleBean.getTerms().stream().map(DictionaryBean::getId).collect(Collectors.toList()));
        }

        //未选择班型，按用户所在组或自己的权限进行查询
        if (StringUtils.isEmpty(condition.getCourseTypeId())) {
            condition.setCourseTypeIds(CollectionUtils.isEmpty(userDataRuleBean.getCourseTypes()) ? new ArrayList<>() : userDataRuleBean.getCourseTypes().stream().map(DictionaryBean::getId).collect(Collectors.toList()));
        }

        //未选择年级，按用户所在组或自己权限进行查询
        if (StringUtils.isEmpty(condition.getGradeId())) {
            condition.setGradeIds(CollectionUtils.isEmpty(userDataRuleBean.getGrades()) ? new ArrayList<>() : userDataRuleBean.getGrades().stream().map(DictionaryBean::getId).collect(Collectors.toList()));
        }
    }
}
